<template>
  <div class="bg">
    <div style="width: 350px;background-color: #fff; /*opacity: 0.9;*/ border-radius: 5px; box-shadow: 0 0 10px rgba(0,0,0,0.1); padding: 40px 20px">
      <el-form ref="formRef" :model="data.form" :rules="data.rules">

        <div style="margin-bottom: 40px; text-align: center; font-weight: bold;font-size: 24px">欢 迎 登 录</div>
        <el-form-item prop="username">
          <el-input size="large" v-model="data.form.username" autocomplete="off" prefix-icon="User" placeholder="请输入账号" />
        </el-form-item>

        <el-form-item prop="password">
          <el-input size="large" show-password v-model="data.form.password" autocomplete="off" prefix-icon="Lock" placeholder="请输入密码"/>
        </el-form-item>

        <el-form-item prop="role">
          <el-select size="large" style="width: 100%" v-model="data.form.role">
            <el-option label="管理员" value="ADMIN"></el-option>
            <el-option label="普通用户" value="USER"></el-option>
          </el-select>
        </el-form-item>

        <div style="margin-bottom: 20px;">
          <el-button style="width: 100%" size="large" type="primary" @click="login">登 录</el-button>
        </div>

        <div style="text-align: right">
          还没有账号?请<a style="color: #1869e0" href="/register">注册</a>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import axios from 'axios'; // ✅ 正确导入 axios
import router from "@/router/index.js";
import request from '@/utils/request'; // ✅ 从你的工具目录导入

const formRef = ref();
const data = reactive({
  form: {
    username: '',
    password: '',
    role: 'ADMIN'
  },
  rules: {
    username: [
      { required: true, message: '请输入账号', trigger: 'blur' },
      { min: 3, message: '账号最少3位', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' }
    ]
  }
});

const login = () => {
  formRef.value.validate((valid) => {
    if (valid) { // 验证通过才发送请求
      request.post('/login', data.form).then(res => {
        if (res.code === '200') {
          localStorage.setItem("code2025_user", JSON.stringify(res.data || {}));
          ElMessage.success('登录成功');
          // router.push('/')
          if('USER' === res.data.role) {
            location.href = '/font/home'
          }else{
            router.push('/');
          }
        } else {
          ElMessage.error(res.msg);
        }
      });
    } else {
      ElMessage.error('请检查输入是否合法');
      return false;
    }
  });
};

</script>

<style scoped> /*限定在当前的页面 在css中 //是非法的 */
.bg {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  background-image: url("@/assets/imgs/登录.png");
  background-size: cover;
}

/* 修改背景颜色 */
.bg {
  background-color: #f17df0;
}

/* 修改表单区域的颜色 */
.bg div {
  background-color: #ffffff;
  color: #333333;
}
</style>